# -*- coding : utf_8 -*-
"""
=======================================
Time : 2025/4/9 19:17
Author : 田霄汉
Email : 522989570@qq.com
File : pandas_class05.py
Project : pandas_demo
Function : 字段运算
=======================================
"""

from config.setting import file_path
import pandas as pd

excel05_path = file_path['excel_path'] + '/excel05.xlsx'
books = pd.read_excel(io=excel05_path,
                      skiprows=3,
                      usecols='C:G',
                      index_col='ID')


# # 两个列进行运算
# # 第一种方法
# books['TOTAL'] = books['AMOUNT'] * books['PRICE']
# print(books)

# 第二种方法，如果不想从头到尾进行运算就按照这个方法
for i in range(3, 8):
    books['TOTAL'].at[i] = books['AMOUNT'].at[i] * books['PRICE'].at[i]
print(books)


# # AMOUNT每个产品加20本数量
# # 第一种方法
# books['AMOUNT'] = books['AMOUNT'] + 20
# print(books)

# 第二种方法，运用series的apply方法实现
def add_20(x):
    return x + 20
books['AMOUNT'] = books['AMOUNT'].apply(add_20)
print(books)

# # 第三种方法，lambda表达式也可以实现
# # books['AMOUNT'] = books['AMOUNT'].apply(lambda x: x + 20)
# # print(books)
